package controller;

import java.security.InvalidParameterException;
import java.util.Collection;

import model.Speed;

/**
 * @author samuel
 * 
 */
public class SpeedLimitValidator implements Validator<Speed> {

	int[] validSpeedMeterPerSecond = { 5, 10, 15, 20, 25, 30 };

	/**
	 * @param speed
	 * @return
	 */
	public boolean isValid(Speed speed) {
		for (int meterPerSecond : validSpeedMeterPerSecond) {
			if (speed.getMeterPerSecond() == meterPerSecond) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Will throw an exception if the parameter is invalid.
	 * 
	 * @param carSpeed
	 */
	public void validate(Speed carSpeed) {

		if (!isValid(carSpeed)) {
			String errorMessage = "The folowing speed "
					+ carSpeed.getMeterPerSecondString()
					+ " does not meet the requirement, the Speed must be within the folowing limits"
					+ validSpeedMeterPerSecond;
			throw new InvalidParameterException(errorMessage);
		}
	}

	@Override
	public void validate(Collection<Speed> values) {
		for (Speed speed : values) {
			validate(speed);
		}

	}
}
